class Solution {
public:
    bool isHappy(int n) {
        vector<int> v;
        while (n != 1)
        {
            int count = 0;
            while (n)
            {
                count += (n % 10) * (n % 10);
                n /= 10;
            }
            if (count == 1)
            {
                return true;
            }
            if (std::find(v.begin(), v.end(), count) != v.end())
            {
                return false;
            }
            v.push_back(count);
            n = count;
        }
        return true;
    }
};